package cn.hunau.bookstore.dao.impl;

import java.sql.ResultSet;

import cn.hunau.bookstore.dao.AdminDao;
import cn.hunau.bookstore.exception.DaoException;
import cn.hunau.bookstore.pojo.Admin;
import cn.hunau.bookstore.utils.ConnectionUtils;
import cn.hunau.bookstore.utils.RowMapper;

/**
 * @author HuangBo
 *
 *         2022-01-18 15:18:40
 *
 */
public class AdminDaoImpl implements AdminDao {
	@Override
	public Admin queryByEmailAndPassword(String email, String password) throws DaoException {
		final String sql = "select id,username,email,logintime,role from t_admin where email=? and password=md5(?)";
		try {
			Admin admin = ConnectionUtils.query(sql, new RowMapper<Admin>() {
				@Override
				public Admin rowMapper(ResultSet rs) throws Exception {
					Admin a = null;
					if (rs.next()) {
						a = new Admin();
						a.setId(rs.getInt("id"));
						a.setUsername(rs.getString("username"));
						a.setEmail(rs.getString("email"));
						a.setLoginTime(rs.getTimestamp("logintime").toLocalDateTime());
						a.setRole(rs.getString("role"));
					}
					return a;
				}
			}, email, password);
			return admin;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
}
